package com.running.base.run.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStreamReader;
import java.util.Properties;

/**
 * @Description 配置文件处理
 * @Author lihao05
 */
public class PropertyUtils {

    private static final Logger LOGGER = LoggerFactory.getLogger(PropertyUtils.class);


    /**
     * 获取properties 中某个key的值
     * @param properties propeties 对象
     * @param key propeties获取value 的 key
     * @return
     */
    public static String getValueByKey(Properties properties , String key){

        String resStr = "" ;
        Object resObj = properties.get(key);
        if (resObj != null){
            resStr = resObj.toString();
        }
        return resStr;
    }

    /**
     * 获取properties 中某个key的值
     * @param properties propeties 对象
     * @param key propeties获取value 的 key
     * @return
     */
    public static String getValueByKey(Properties properties , Integer key){

        return getValueByKey(properties,String.valueOf(key));
    }

    /**
     * 加载配置文件,返回Properties对象
     */
    public static Properties getProp(String propFileName, String key) throws Exception {
        Properties prop = new Properties();
        prop.load(new InputStreamReader(AjaxResultUtil.class.getClassLoader().getResourceAsStream(propFileName), "UTF-8"));
        return prop;
    }
    public static String getValueByKeyUTF8(Properties properties , Integer key){

        String resStr = "" ;
        Object resObj = properties.get(String.valueOf(key));
        if (resObj != null){
            resStr = resObj.toString();
            try {
                resStr = new String(resObj.toString().getBytes("ISO8859-1"), "UTF-8");
            }catch (Exception e){
                LOGGER.error("charset change error",e);
            }
        }
        return resStr;
    }

}
